Programming Languages Section

Groovy, What is It ?

Card image
Post by Amina Delali, July 06th, 2022

Some Facts

Apache Groovy is an object oriented programming language with scripting capabilities. It was created to be used alongside with Java language, so it naturally compiles to a JVM (Java Virtual Machine) byte code. Its java-like syntax is simple, concise, and easy to learn.

The language is optionally typed and dynamic. And it incorporates; beside its scripting capabilities, some interesting features as functional programming, meta-programming, and domain-specific authoring.



How to install it

  • on Windows:

    First you must have Java already installed in your system. If it is not the case, you can follow the instructions available here to install it.

    Now, to install Groovy you will simply download and run the installer:

    1. download the installer from this page. Or simply go this installation page, and download the installer corresponding to the latest stable version. 
    2. run the installer and follow the instructions. You will have the choice between 3 types of installation: typical, custom, and complete. I selected the complete one.
    3. you can check the installation just by opening the Groovy shell from the shortcut created on your desktop, or by opening the command prompt and running the following command: groovy -v

  • on ubuntu :

    Again, you must have Java installed in your system. If it is not the case, you can check our page here.
    To install Groovy, we are going to use the Software Development Kit Manager (SDKMAN). Formerly known as GVM (Groovy enVironment Manager), the tool is used to manage parallel versions of multiple SDK (Software Development Kits). Open the terminal and:

    1. run the command:  sudo apt update
    2. download and install SDKMAN by downloading and launching the installer script (you must have curl already installed in your system):sudo curl -s "https://get.sdkman.io" | bash
    3. to be able to use the sdk commands, run source "/home/$USER/.sdkman/bin/sdkman-init.sh"
    4. install groovy by running this command: sdk install groovy
    5. you can verify your installation by checking the installed version: groovy -version

The Hello World Example

  1. Before starting out, I just want to add, that the following steps were tested on Ubuntu. First, create a new file in the home directory and save it under the name "hello.groovy"
  2. Write and save in that file the following code:
    println "Hello World!"
  3.  To run the code as a script, open the terminal from the home folder and run the following command: groovy hello.groovy
  4.  To run the code as a class, first you have to compile the file. From the same terminal write: groovyc hello.groovy It will generate the class "hello.class"
  5.  Now you can run it using Java. To do so, you need first to know the version's number of your installed groovy (or the selected one). Just run on the terminal: groovy -version For example, if the version you installed is: groovy-4.0.3 . So we will write the following command: java -cp .:$GROOVY_HOME/lib/groovy-4.0.3.jar hello
    6. If you have a different version you will have simply to replace the 4.0.3 in groovy-4.0.3.jar by the number of your version.


Something to say ?

If you want to add something about the Groovy language or about this post, please feel free to do so by commenting below 🙂 .